package com.zhuojy.equity.mall.admin.web.controller.salary;/**
 * @description: TODO
 * @author chen
 * @date 2024/4/6 19:40
 * @version 1.0
 */

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhuojy.equity.mall.admin.common.entity.bo.salary.PayrollItemsBO;
import com.zhuojy.equity.mall.admin.common.entity.po.salary.PayrollItemsPO;
import com.zhuojy.equity.mall.admin.common.pojo.ResultEntity;
import com.zhuojy.equity.mall.admin.common.utils.ExcelUtils;
import com.zhuojy.equity.mall.admin.service.salary.PayrollItemsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @description: 薪资项目模块
 * @author chen
 * @date 2024/4/6 19:40
 * @version 1.0
 */
@RestController
@RequestMapping("payrollItems")
@RequiredArgsConstructor
@Api(tags = "薪资模块")
public class PayrollItemsController {

    private final PayrollItemsService payrollItemsService;

    @PostMapping("addPayrollItem")
    @ApiOperation(value = "添加薪资项目",httpMethod = "POST",notes = "添加薪资项目")
    public ResultEntity addPayrollItem(@RequestBody PayrollItemsPO payrollItemsPO){
        try {
            payrollItemsService.save(payrollItemsPO);
            return ResultEntity.OK("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.ERROR("新增失败");
        }
    }
    @PostMapping("getPayrollItemsList")
    @ApiOperation(value = "添加薪资项目",httpMethod = "POST",notes = "添加薪资项目")
    public ResultEntity getPayrollItemsList(@RequestBody PayrollItemsBO payrollItemsBO){
        try {
            Map map = payrollItemsService.getPayrollItemsList(payrollItemsBO);
            return ResultEntity.OK(map);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.ERROR("查询失败");
        }
    }
    @PostMapping("updatePayrollItem")
    @ApiOperation(value = "修改薪资项目",httpMethod = "POST",notes = "修改薪资项目")
    public ResultEntity updatePayrollItem(@RequestBody PayrollItemsPO payrollItemsPO){
        try {
            payrollItemsService.updateById(payrollItemsPO);
            return ResultEntity.OK("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.ERROR("修改失败");
        }
    }
    @GetMapping("findPayrollItems")
    @ApiOperation(value = "回显薪资项目",httpMethod = "GET",notes = "回显薪资项目")
    public ResultEntity findPayrollItems(@RequestParam("id") Long id){
        try {
            PayrollItemsPO payrollItemsPO = payrollItemsService.findPayrollItems(id);
            return ResultEntity.OK(payrollItemsPO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.ERROR("回显失败");
        }
    }
    @GetMapping("deletePayrollItems")
    @ApiOperation(value = "删除薪资项目",httpMethod = "GET",notes = "删除薪资项目")
    public ResultEntity deletePayrollItems(@RequestParam("id") Long id){
        try {
            payrollItemsService.remove(Wrappers.lambdaQuery(PayrollItemsPO.class).eq(PayrollItemsPO::getId,id));
            return ResultEntity.OK("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.ERROR("删除失败");
        }
    }
    @GetMapping("exprotPoi")
    @ApiOperation(value = "导出薪资项目",httpMethod = "GET",notes = "导出薪资项目")
    public void exprotPoi(HttpServletResponse response){

        List<PayrollItemsPO> list1 = payrollItemsService.list();
        ExcelUtils.export(response,"薪资项目.xlsx",list1,PayrollItemsPO.class);
    }
    @PostMapping("importPoi")
    @ApiOperation(value = "导入薪资项目",httpMethod = "POST",notes = "导入薪资项目")
    public ResultEntity importPoi(MultipartFile file) throws Exception {
        try {
            List<PayrollItemsPO> payrollItemsPOS = ExcelUtils.readMultipartFile(file, PayrollItemsPO.class);
            for (PayrollItemsPO payrollItemsPO : payrollItemsPOS) {
                if ("税前加项".equals(payrollItemsPO.getProjectType())){
                    payrollItemsPO.setProjectType("1");
                }else if("税前减项".equals(payrollItemsPO.getProjectType())){
                    payrollItemsPO.setProjectType("2");
                }else if("税费项".equals(payrollItemsPO.getProjectType())){
                    payrollItemsPO.setProjectType("3");
                }else if("税后加项".equals(payrollItemsPO.getProjectType())){
                    payrollItemsPO.setProjectType("4");
                }else if("税后减项".equals(payrollItemsPO.getProjectType())){
                    payrollItemsPO.setProjectType("5");
                }else if("统计项".equals(payrollItemsPO.getProjectType())){
                    payrollItemsPO.setProjectType("6");
                }else {
                    payrollItemsPO.setProjectType("7");
                }
                if ("原始数据".equals(payrollItemsPO.getRoundingRules())){
                    payrollItemsPO.setRoundingRules("1");
                }else if("四舍五入".equals(payrollItemsPO.getRoundingRules())){
                    payrollItemsPO.setRoundingRules("2");
                }else if("向上舍入数字".equals(payrollItemsPO.getRoundingRules())){
                    payrollItemsPO.setRoundingRules("3");
                }else{
                    payrollItemsPO.setRoundingRules("4");
                }
                if ("启用".equals(payrollItemsPO.getState())){
                    payrollItemsPO.setState("1");
                }else{
                    payrollItemsPO.setState("2");
                }
                System.out.println(payrollItemsPO);
                payrollItemsService.save(payrollItemsPO);
            }
            return ResultEntity.OK("导入成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.OK("导入失败");
        }
    }
}
